package aod.android.com.bernay.mine.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;

import java.io.IOException;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseFragment;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.mine.activity.AboutUsActivity;
import aod.android.com.bernay.mine.activity.AlbumActivity;
import aod.android.com.bernay.mine.activity.CenterActivity;
import aod.android.com.bernay.mine.activity.DynamicActivity;
import aod.android.com.bernay.mine.activity.FeedBackActivity;
import aod.android.com.bernay.mine.activity.MallActivity;
import aod.android.com.bernay.mine.activity.OnlineActivity;
import aod.android.com.bernay.mine.activity.ProblemActivity;
import aod.android.com.bernay.mine.activity.SettingActivity;
import aod.android.com.bernay.mine.module.DynamicBean;
import aod.android.com.bernay.mine.module.MineUserInfo;
import aod.android.com.bernay.util.SharePreUtils;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/3/31 15:27
 * @Email jamesKing0812@gmail.com
 */
public class MineFragment extends BernayBaseFragment implements View.OnClickListener {

    //设置按钮
    @HiView(R.id.setting)
    private ImageView mSetting;

    //我的头像
    @HiView(R.id.img_mineAvatar)
    private ImageView mineAvatar;

    //我的头像
    @HiView(R.id.tv_mineName)
    private TextView mineName;

    //我的相册
    @HiView(R.id.ll_album)
    private LinearLayout mAlbum;

    //我的动态
    @HiView(R.id.ll_dynamic)
    private LinearLayout mDynamic;

    //常见问题
    @HiView(R.id.rl_commonProblem)
    private RelativeLayout mCommonProblem;

    @HiView(R.id.rl_serviceCenter)
    private RelativeLayout mServiceCenter;

    @HiView(R.id.rl_feedback)
    private RelativeLayout mFeedback;

    @HiView(R.id.rl_merchantMall)
    private RelativeLayout mMerchantMall;

    @HiView(R.id.rl_aboutUs)
    private RelativeLayout mAboutUs;

    @HiView(R.id.rl_onlineService)
    private RelativeLayout mOnlineService;

    private MineUserInfo mineUserInfo;

    private RequestOptions options;

    @Override
    protected int initLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {

        options = RequestOptions.circleCropTransform()
                .placeholder(R.drawable.icon_head_portrait)
                .error(R.drawable.icon_head_portrait)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                .skipMemoryCache(true);

        getUserInfo();

        mSetting.setOnClickListener(this);
        mAlbum.setOnClickListener(this);
        mDynamic.setOnClickListener(this);
        mCommonProblem.setOnClickListener(this);
        mServiceCenter.setOnClickListener(this);
        mFeedback.setOnClickListener(this);
        mMerchantMall.setOnClickListener(this);
        mAboutUs.setOnClickListener(this);
        mOnlineService.setOnClickListener(this);
    }

    /**
     * 获取用户信息
     */
    private void getUserInfo() {
        String userId = SharePreUtils.newInstance(getActivity()).getUserId();
        String token =  SharePreUtils.newInstance(getActivity()).getToken();
        BernayApi.http().getUserInfo(userId, token,new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                mineUserInfo = new Gson().fromJson(json, MineUserInfo.class);
                if (mineUserInfo.getStatus() == 1){
                    if (!mineUserInfo.getData().get(0).getHeadPicUrl().equals("")){
                        Glide.with(getActivity())
//                                .load(mineUserInfo.getData().get(0).getHeadPicUrl())
                                .load("http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg")
                                .apply(options)
                                .into(mineAvatar);
                        mineName.setText(mineUserInfo.getData().get(0).getNickName());
                    }
                }
            }

            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });
    }

    @Override
    public void onClick(View v) {
        Bundle bundle =  new Bundle();
        switch (v.getId()){
            case R.id.setting:
                bundle.putString("headPicUrl","http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg");
                bundle.putString("nickName","张小凡");
                bundle.putString("tellPhone","15618776065");
                bundle.putString("realName","鬼厉");
                goActivity(SettingActivity.class,bundle);
                break;
            case R.id.ll_album:
                goActivity(AlbumActivity.class);
                break;
            case R.id.ll_dynamic:
                goActivity(DynamicActivity.class);
                break;
            case R.id.rl_commonProblem:
                goActivity(ProblemActivity.class);
                break;
            case R.id.rl_serviceCenter:
                bundle.putString("","");
                goActivity(CenterActivity.class);
                break;
            case R.id.rl_feedback:
                goActivity(FeedBackActivity.class);
                break;
            case R.id.rl_merchantMall:
                goActivity(MallActivity.class);
                break;
            case R.id.rl_aboutUs:
                goActivity(AboutUsActivity.class);
                break;
            case R.id.rl_onlineService:
                goActivity(OnlineActivity.class);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
